/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TieBreakTrainingTrainedByCoach.java
 *
 * Created on 23-03-2011, 17:18:01
 */
package tiebreak.UI;

import com.microsoft.sqlserver.jdbc.SQLServerException;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import tiebreak.BE.TrainingteamJoinTeam;
import tiebreak.BL.Training;

/**
 *
 * @author henrikhansen
 */
public class TieBreakTrainingTrainedByCoach extends javax.swing.JPanel {

    int teamID;

    /** Creates new form TieBreakTrainingTrainedByCoach */
    public TieBreakTrainingTrainedByCoach() throws SQLException {
        initComponents();
        listTeams();
        MouseListener ml = new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 1) {
                    try {
                        for (TrainingteamJoinTeam teamInfo : Training.getTeamsInTraining()) {
                            if (teamInfo.getTeamName().equals(lstTeams.getSelectedValue().toString())) {
                                lblCoache.setText(teamInfo.getFirstName() + " " + teamInfo.getSureName());
                                getMembersInTeam(teamInfo.getTeamID());
                            }
                        }
                    } catch (SQLException ex) {
                        Logger.getLogger(TieBreakTrainingTrainedByCoach.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }

            private void getMembersInTeam(int teamID) throws SQLServerException, SQLException {
                DefaultListModel model = new DefaultListModel();
                for (TrainingteamJoinTeam ttj : Training.getAllMembersByTeam(teamID)) {
                    model.addElement(ttj.getFirstName() + " " + ttj.getSureName());
                }
                lstTeamMembers.setModel(model);
            }
        };
        lstTeams.addMouseListener(ml);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        lstTeams = new javax.swing.JList();
        jLabel1 = new javax.swing.JLabel();
        lblCoache = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        lstTeamMembers = new javax.swing.JList();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();

        jScrollPane1.setViewportView(lstTeams);

        jLabel1.setFont(new java.awt.Font("Lucida Grande", 1, 13));
        jLabel1.setText("Coach:");

        jScrollPane2.setViewportView(lstTeamMembers);

        jLabel2.setFont(new java.awt.Font("Lucida Grande", 1, 13));
        jLabel2.setText("Teams:");

        jLabel3.setFont(new java.awt.Font("Lucida Grande", 1, 13));
        jLabel3.setText("Members:");

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(jLabel1)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(lblCoache))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(layout.createSequentialGroup()
                                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 172, Short.MAX_VALUE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED))
                            .add(layout.createSequentialGroup()
                                .add(jLabel2)
                                .add(138, 138, 138)))
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 192, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(jLabel3))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(20, 20, 20)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2)
                    .add(jLabel3))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 232, Short.MAX_VALUE)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 232, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(lblCoache))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel lblCoache;
    private javax.swing.JList lstTeamMembers;
    private javax.swing.JList lstTeams;
    // End of variables declaration//GEN-END:variables

    private void listTeams() throws SQLException {
        DefaultListModel model = new DefaultListModel();
        for (TrainingteamJoinTeam teams : Training.getAllTeamsInTraining()) {
            model.addElement(teams.getTeamName());
        }
        lstTeams.setModel(model);
    }
}
